/**
 * FileName: DetailsController
 * Author:   秋水浮萍
 * Date:     2020/7/21 18:50
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.crm.crm.controller;

import com.crm.crm.service.CrmPartnerStatService;
import com.crm.dto.PartnerDepNameDTO;
import com.crm.dto.PartnerSelfDTO;
import com.crm.model.FormsInfo;
import com.crm.model.JsonMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author 秋水浮萍
 * @create 2020/7/21
 * @since 1.0.0
 */
@RestController
@Api(value = "管理员绩效管理导出接口")
@RequestMapping("/admin/detail")
public class CrmSysDetailsController {
    @Autowired
    private CrmPartnerStatService statService;

    /**
     * 周导出
     *
     * @param times
     * @return
     */
    @ApiOperation(value = "周导出")
    @RequestMapping("exportWeek")
    public ResponseEntity<byte[]> exportWeek(int times) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //获取本周周一
        Calendar cal = new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        Date first = cal.getTime();

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(first);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(first);
        int days = Calendar.DAY_OF_WEEK;
        calendar.add(Calendar.DAY_OF_MONTH, -days * times);
        String ben = simpleDateFormat.format(calendar.getTime());
        calendar1.add(Calendar.DAY_OF_MONTH, -days * times + 7);
        String end = simpleDateFormat.format(calendar1.getTime());

        //create excel object
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook();
        List<PartnerSelfDTO> mas = statService.partnerWeekCount(ben, end);
        Map<String, Integer> map = new HashMap<>();
        for (PartnerSelfDTO dos : mas
        ) {
            map.put(dos.getTrueName(), dos.getCounts());
        }


        //sheet1
        XSSFSheet sheet1 = hssfWorkbook.createSheet("个人");
        List<PartnerSelfDTO> list1 = statService.partnerWeek(ben, end);
        String[] headerList1 = {"名次", "姓名", "部门", "周新增", "资产伙伴", "资金伙伴", "司法伙伴", "政府伙伴", "退出伙伴", "同业伙伴"};
        XSSFRow row = sheet1.createRow(0);
        for (int i = 0; i < headerList1.length; i++) {
            row.createCell(i).setCellValue(headerList1[i]);
        }

        //添加之后的部门进行排序
        Collections.sort(list1, new Comparator<PartnerSelfDTO>() {
            @Override
            public int compare(PartnerSelfDTO o1, PartnerSelfDTO o2) {
                return o2.getCounts() - o1.getCounts();
            }
        });
        int i = 0;
        for (PartnerSelfDTO stat : list1) {
            int j = 0;
            XSSFRow row1 = sheet1.createRow(++i);
            row1.createCell(j++).setCellValue(i);
            row1.createCell(j++).setCellValue(stat.getTrueName());
            row1.createCell(j++).setCellValue(stat.getDepName());
            row1.createCell(j++).setCellValue(stat.getAsset() + stat.getFund() + stat.getJustice() + stat.getGov() + stat.getExits() + stat.getPeer());
            row1.createCell(j++).setCellValue(stat.getAsset());
            row1.createCell(j++).setCellValue(stat.getFund());
            row1.createCell(j++).setCellValue(stat.getJustice());
            row1.createCell(j++).setCellValue(stat.getGov());
            row1.createCell(j++).setCellValue(stat.getExits());
            row1.createCell(j++).setCellValue(stat.getPeer());
        }


        //sheet2
        XSSFSheet sheet2 = hssfWorkbook.createSheet("部门");
        List<PartnerSelfDTO> list2 = statService.partnerWeek(ben, end);
        List<PartnerDepNameDTO> all = new ArrayList<>();
        List<FormsInfo> listDepId = statService.depId();
        String[] headerList2 = {"名次", "部门", "周新增", "资产伙伴", "资金伙伴", "司法伙伴", "政府伙伴", "退出伙伴", "同业伙伴"};
        XSSFRow row2 = sheet2.createRow(0);
        for (int j = 0; j < headerList2.length; j++) {
            row2.createCell(j).setCellValue(headerList2[j]);
        }
        for (PartnerSelfDTO p : list2
        ) {
            p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
        }
        for (FormsInfo info : listDepId
        ) {
            int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, cu = 0;
            for (PartnerSelfDTO dto : list2
            ) {
                if (info.getDepName().equals(dto.getDepName())) {
                    cu += dto.getCounts();
                    a += dto.getAsset();
                    b += dto.getFund();
                    c += dto.getJustice();
                    d += dto.getGov();
                    e += dto.getExits();
                    f += dto.getPeer();
                }
            }
            PartnerDepNameDTO ds = new PartnerDepNameDTO(info.getDepName(), info.getDepId(), cu, a, b, e, c, f, d);
            all.add(ds);
        }
        //添加之后的部门进行排序
        Collections.sort(all, new Comparator<PartnerDepNameDTO>() {
            @Override
            public int compare(PartnerDepNameDTO o1, PartnerDepNameDTO o2) {
                return o2.getCounts() - o1.getCounts();
            }
        });
        int i1 = 0;
        for (PartnerDepNameDTO stat : all) {
            int j = 0;
            XSSFRow row1 = sheet2.createRow(++i1);
            row1.createCell(j++).setCellValue(i1);
            row1.createCell(j++).setCellValue(stat.getDepName());
            row1.createCell(j++).setCellValue(stat.getAsset() + stat.getFund() + stat.getJustice() + stat.getGov() + stat.getExits() + stat.getPeer());
            row1.createCell(j++).setCellValue(stat.getAsset());
            row1.createCell(j++).setCellValue(stat.getFund());
            row1.createCell(j++).setCellValue(stat.getJustice());
            row1.createCell(j++).setCellValue(stat.getGov());
            row1.createCell(j++).setCellValue(stat.getExits());
            row1.createCell(j++).setCellValue(stat.getPeer());
        }

        try {
            hssfWorkbook.write(os);
            hssfWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        // headers.add("Content-Type", "application/octet-stream;charset=utf-8");
        headers.add("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
        headers.add("Connection", "close");
        headers.add("Accept-Ranges", "bytes");
        headers.add("Access-Control-Expose-Headers", "Content-Disposition");
        String myFileName = URLEncoder.encode("周统计.xlsx", Charset.forName("utf-8"));
        headers.add("Content-Disposition", "attachment;filename=" + myFileName);
        //   + new String( "汇总.xlsx".getBytes(Charset.forName("UTF-8")), Charset.forName("ISO-8859-1")));
        return ResponseEntity
                .ok()
                .headers(headers)
                //.contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(os.toByteArray());
    }

    /**
     * 月导出
     *
     * @param times
     * @return
     */
    @ApiOperation(value = "月导出")
    @RequestMapping("exportMouth")
    public ResponseEntity<byte[]> exportMouth(String times) {
        //create excel object
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook();

        String[] ts = times.split("年");
        ts[1] = (String) ts[1].subSequence(0, 2);
        List<PartnerSelfDTO> mas = statService.partnerMonthCount(ts[0], ts[1]);
        Map<String, Integer> map = new HashMap<>();
        for (PartnerSelfDTO dos : mas) {
            map.put(dos.getTrueName(), dos.getCounts());
        }

        //sheet1
        XSSFSheet sheet1 = hssfWorkbook.createSheet("个人");
        List<PartnerSelfDTO> list1 = statService.partnerMonth(ts[0], ts[1]);
        String[] headerList1 = {"名次", "姓名", "部门", "周新增", "资产伙伴", "资金伙伴", "司法伙伴", "政府伙伴", "退出伙伴", "同业伙伴"};
        XSSFRow row = sheet1.createRow(0);
        for (int i = 0; i < headerList1.length; i++) {
            row.createCell(i).setCellValue(headerList1[i]);
        }
        //添加之后的部门进行排序
        Collections.sort(list1, new Comparator<PartnerSelfDTO>() {
            @Override
            public int compare(PartnerSelfDTO o1, PartnerSelfDTO o2) {
                return o2.getCounts() - o1.getCounts();
            }
        });
        int i = 0;
        for (PartnerSelfDTO stat : list1) {
            int j = 0;
            XSSFRow row1 = sheet1.createRow(++i);
            row1.createCell(j++).setCellValue(i);
            row1.createCell(j++).setCellValue(stat.getTrueName());
            row1.createCell(j++).setCellValue(stat.getDepName());
            row1.createCell(j++).setCellValue(stat.getAsset() + stat.getFund() + stat.getJustice() + stat.getGov() + stat.getExits() + stat.getPeer());
            row1.createCell(j++).setCellValue(stat.getAsset());
            row1.createCell(j++).setCellValue(stat.getFund());
            row1.createCell(j++).setCellValue(stat.getJustice());
            row1.createCell(j++).setCellValue(stat.getGov());
            row1.createCell(j++).setCellValue(stat.getExits());
            row1.createCell(j++).setCellValue(stat.getPeer());
        }


        //sheet2
        XSSFSheet sheet2 = hssfWorkbook.createSheet("部门");
        List<PartnerSelfDTO> list2 = statService.partnerMonth(ts[0], ts[1]);
        List<PartnerDepNameDTO> all = new ArrayList<>();
        List<FormsInfo> listDepId = statService.depId();
        String[] headerList2 = {"名次", "部门", "周新增", "资产伙伴", "资金伙伴", "司法伙伴", "政府伙伴", "退出伙伴", "同业伙伴"};
        XSSFRow row2 = sheet2.createRow(0);
        for (int j = 0; j < headerList2.length; j++) {
            row2.createCell(j).setCellValue(headerList2[j]);
        }

        for (PartnerSelfDTO p : list2
        ) {
            p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
        }
        for (FormsInfo info : listDepId
        ) {
            int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, cu = 0;
            for (PartnerSelfDTO dto : list2
            ) {
                if (info.getDepName().equals(dto.getDepName())) {
                    cu += dto.getCounts();
                    a += dto.getAsset();
                    b += dto.getFund();
                    c += dto.getJustice();
                    d += dto.getGov();
                    e += dto.getExits();
                    f += dto.getPeer();
                }
            }
            PartnerDepNameDTO ds = new PartnerDepNameDTO(info.getDepName(), info.getDepId(), cu, a, b, e, c, f, d);
            all.add(ds);
        }

        //添加之后的部门进行排序
        Collections.sort(all, new Comparator<PartnerDepNameDTO>() {
            @Override
            public int compare(PartnerDepNameDTO o1, PartnerDepNameDTO o2) {
                return o2.getCounts() - o1.getCounts();
            }
        });
        int i1 = 0;
        for (PartnerDepNameDTO stat : all) {
            int j = 0;
            XSSFRow row1 = sheet2.createRow(++i1);
            row1.createCell(j++).setCellValue(i1);
            row1.createCell(j++).setCellValue(stat.getDepName());
            row1.createCell(j++).setCellValue(stat.getAsset() + stat.getFund() + stat.getJustice() + stat.getGov() + stat.getExits() + stat.getPeer());
            row1.createCell(j++).setCellValue(stat.getAsset());
            row1.createCell(j++).setCellValue(stat.getFund());
            row1.createCell(j++).setCellValue(stat.getJustice());
            row1.createCell(j++).setCellValue(stat.getGov());
            row1.createCell(j++).setCellValue(stat.getExits());
            row1.createCell(j++).setCellValue(stat.getPeer());
        }

        try {
            hssfWorkbook.write(os);
            hssfWorkbook.close();
        } catch (
                IOException e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
        headers.add("Connection", "close");
        headers.add("Accept-Ranges", "bytes");
        headers.add("Access-Control-Expose-Headers", "Content-Disposition");
        String myFileName = URLEncoder.encode("月统计.xlsx", Charset.forName("utf-8"));
        headers.add("Content-Disposition", "attachment;filename=" + myFileName);
        return ResponseEntity
                .ok()
                .headers(headers)
                .body(os.toByteArray());
    }

    /**
     * 季度导出
     *
     * @param times
     * @return
     */
    @ApiOperation(value = "季度导出")
    @RequestMapping("exportQuqrter")
    public ResponseEntity<byte[]> exportQuqrter(String times) {
        //create excel object
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XSSFWorkbook hssfWorkbook = new XSSFWorkbook();
        String[] ts = times.split("年");
        int quar = 0;
        if ("第1季度".equals(ts[1])) {
            quar = 1;
        } else if ("第2季度".equals(ts[1])) {
            quar = 2;
        } else if ("第3季度".equals(ts[1])) {
            quar = 3;
        } else if ("第4季度".equals(ts[1])) {
            quar = 4;
        }
        List<PartnerSelfDTO> mas = statService.partnerQuarterCount(ts[0], quar);
        Map<String, Integer> map = new HashMap<>();
        for (PartnerSelfDTO dos : mas
        ) {
            map.put(dos.getTrueName(), dos.getCounts());
        }

        //sheet1
        XSSFSheet sheet1 = hssfWorkbook.createSheet("个人");
        List<PartnerSelfDTO> list1 = statService.partnerQuarter(ts[0], quar);
        String[] headerList1 = {"名次", "姓名", "部门", "季度新增", "资产伙伴", "资金伙伴", "司法伙伴", "政府伙伴", "退出伙伴", "同业伙伴"};
        XSSFRow row = sheet1.createRow(0);
        for (int i = 0; i < headerList1.length; i++) {
            row.createCell(i).setCellValue(headerList1[i]);
        }

        //添加之后的部门进行排序
        Collections.sort(list1, new Comparator<PartnerSelfDTO>() {
            @Override
            public int compare(PartnerSelfDTO o1, PartnerSelfDTO o2) {
                return o2.getCounts() - o1.getCounts();
            }
        });
        int i = 0;
        for (PartnerSelfDTO stat : list1) {
            int j = 0;
            XSSFRow row1 = sheet1.createRow(++i);
            row1.createCell(j++).setCellValue(i);
            row1.createCell(j++).setCellValue(stat.getTrueName());
            row1.createCell(j++).setCellValue(stat.getDepName());
            row1.createCell(j++).setCellValue(stat.getAsset() + stat.getFund() + stat.getJustice() + stat.getGov() + stat.getExits() + stat.getPeer());
            row1.createCell(j++).setCellValue(stat.getAsset());
            row1.createCell(j++).setCellValue(stat.getFund());
            row1.createCell(j++).setCellValue(stat.getJustice());
            row1.createCell(j++).setCellValue(stat.getGov());
            row1.createCell(j++).setCellValue(stat.getExits());
            row1.createCell(j++).setCellValue(stat.getPeer());
        }


        //sheet2
        XSSFSheet sheet2 = hssfWorkbook.createSheet("部门");

        List<PartnerSelfDTO> list2 = statService.partnerQuarter(ts[0], quar);
        List<PartnerDepNameDTO> all = new ArrayList<>();
        List<FormsInfo> listDepId = statService.depId();
        String[] headerList2 = {"名次", "部门", "周新增", "资产伙伴", "资金伙伴", "司法伙伴", "政府伙伴", "退出伙伴", "同业伙伴"};
        XSSFRow row2 = sheet2.createRow(0);
        for (int j = 0; j < headerList2.length; j++) {
            row2.createCell(j).setCellValue(headerList2[j]);
        }
        for (PartnerSelfDTO p : list2
        ) {
            p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
        }
        for (FormsInfo info : listDepId
        ) {
            int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, cu = 0;
            for (PartnerSelfDTO dto : list2
            ) {
                if (info.getDepName().equals(dto.getDepName())) {
                    cu += dto.getCounts();
                    a += dto.getAsset();
                    b += dto.getFund();
                    c += dto.getJustice();
                    d += dto.getGov();
                    e += dto.getExits();
                    f += dto.getPeer();
                }
            }
            PartnerDepNameDTO ds = new PartnerDepNameDTO(info.getDepName(), info.getDepId(), cu, a, b, e, c, f, d);
            all.add(ds);
        }

        //添加之后的部门进行排序
        Collections.sort(all, new Comparator<PartnerDepNameDTO>() {
            @Override
            public int compare(PartnerDepNameDTO o1, PartnerDepNameDTO o2) {
                return o2.getCounts() - o1.getCounts();
            }
        });
        int i1 = 0;
        for (PartnerDepNameDTO stat : all) {
            int j = 0;
            XSSFRow row1 = sheet2.createRow(++i1);
            row1.createCell(j++).setCellValue(i1);
            row1.createCell(j++).setCellValue(stat.getDepName());
            row1.createCell(j++).setCellValue(stat.getAsset() + stat.getFund() + stat.getJustice() + stat.getGov() + stat.getExits() + stat.getPeer());
            row1.createCell(j++).setCellValue(stat.getAsset());
            row1.createCell(j++).setCellValue(stat.getFund());
            row1.createCell(j++).setCellValue(stat.getJustice());
            row1.createCell(j++).setCellValue(stat.getGov());
            row1.createCell(j++).setCellValue(stat.getExits());
            row1.createCell(j++).setCellValue(stat.getPeer());
        }

        try {
            hssfWorkbook.write(os);
            hssfWorkbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        HttpHeaders headers = new HttpHeaders();
        // headers.add("Content-Type", "application/octet-stream;charset=utf-8");
        headers.add("Content-Type", "application/vnd.ms-excel;charset=UTF-8");
        headers.add("Connection", "close");
        headers.add("Accept-Ranges", "bytes");
        headers.add("Access-Control-Expose-Headers", "Content-Disposition");
        String myFileName = URLEncoder.encode("季度统计.xlsx", Charset.forName("utf-8"));
        headers.add("Content-Disposition", "attachment;filename=" + myFileName);
        //   + new String( "汇总.xlsx".getBytes(Charset.forName("UTF-8")), Charset.forName("ISO-8859-1")));
        return ResponseEntity
                .ok()
                .headers(headers)
                //.contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(os.toByteArray());
    }


    /**
     * 周统计
     *
     * @param times
     * @param name
     * @return
     */
    @ApiOperation(value = "周统计")
    @PostMapping("rankWeek")
    public JsonMessage rankWeek(int times, String name) throws ParseException {
        JsonMessage message = new JsonMessage();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        //获取本周周一
        Calendar cal = new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(date);
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        Date first = cal.getTime();

        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(first);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(first);

        int days = Calendar.DAY_OF_WEEK;

        calendar.add(Calendar.DAY_OF_MONTH, -days * times);
        String ben = simpleDateFormat.format(calendar.getTime());

        calendar1.add(Calendar.DAY_OF_MONTH, -days * times + 7);
        String end = simpleDateFormat.format(calendar1.getTime());

        List<PartnerSelfDTO> mas = statService.partnerWeekCount(ben, end);
        Map<String, Integer> map = new HashMap<>();
        for (PartnerSelfDTO dos : mas
        ) {
            map.put(dos.getTrueName(), dos.getCounts());
        }

        if ("个人".equals(name)) {
            //sheet1
            List<PartnerSelfDTO> list1 = statService.partnerWeek(ben, end);

            for (PartnerSelfDTO p : list1
            ) {
                //
                p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
            }
            //添加之后的部门进行排序
            Collections.sort(list1, new Comparator<PartnerSelfDTO>() {
                @Override
                public int compare(PartnerSelfDTO o1, PartnerSelfDTO o2) {
                    return o2.getCounts() - o1.getCounts();
                }
            });
            message.setData(list1);
        }
        if ("部门".equals(name)) {
            //sheet1
            List<PartnerSelfDTO> list1 = statService.partnerWeek(ben, end);
            List<PartnerDepNameDTO> all = new ArrayList<>();
            List<FormsInfo> list = statService.depId();

            for (PartnerSelfDTO p : list1
            ) {
                p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
            }
            for (FormsInfo info : list
            ) {
                int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, cu = 0;
                for (PartnerSelfDTO dto : list1
                ) {
                    if (info.getDepName().equals(dto.getDepName())) {
                        cu += dto.getCounts();
                        a += dto.getAsset();
                        b += dto.getFund();
                        c += dto.getJustice();
                        d += dto.getGov();
                        e += dto.getExits();
                        f += dto.getPeer();
                    }
                }
                PartnerDepNameDTO ds = new PartnerDepNameDTO(info.getDepName(), info.getDepId(), a + b + c + d + e + f, a, b, e, c, f, d);
                all.add(ds);
            }
            //添加之后的部门进行排序
            Collections.sort(all, new Comparator<PartnerDepNameDTO>() {
                @Override
                public int compare(PartnerDepNameDTO o1, PartnerDepNameDTO o2) {
                    return o2.getCounts() - o1.getCounts();
                }
            });
            message.setData(all);
        }
        return message;
    }

    /**
     * 月统计
     *
     * @param times
     * @param name
     * @return
     */
    @ApiOperation(value = "月统计")
    @PostMapping("rankMouth")
    public JsonMessage rankMouth(String times, String name) {
        JsonMessage message = new JsonMessage();
        String[] ts = times.split("年");
        ts[1] = (String) ts[1].subSequence(0, 2);
        List<PartnerSelfDTO> mas = statService.partnerMonthCount(ts[0], ts[1]);
        Map<String, Integer> map = new HashMap<>();
        for (PartnerSelfDTO dos : mas
        ) {
            map.put(dos.getTrueName(), dos.getCounts());
        }
        if ("个人".equals(name)) {
            //sheet1
            List<PartnerSelfDTO> list1 = statService.partnerMonth(ts[0], ts[1]);
            for (PartnerSelfDTO p : list1
            ) {
                p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
            }
            //添加之后的部门进行排序
            Collections.sort(list1, new Comparator<PartnerSelfDTO>() {
                @Override
                public int compare(PartnerSelfDTO o1, PartnerSelfDTO o2) {
                    return o2.getCounts() - o1.getCounts();
                }
            });
            message.setData(list1);
        }
        if ("部门".equals(name)) {
            //sheet1
            List<PartnerSelfDTO> list1 = statService.partnerMonth(ts[0], ts[1]);
            List<PartnerDepNameDTO> all = new ArrayList<>();
            List<FormsInfo> list = statService.depId();


            for (PartnerSelfDTO p : list1
            ) {
                p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
            }
            for (FormsInfo info : list
            ) {
                int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, cu = 0;
                for (PartnerSelfDTO dto : list1
                ) {
                    if (info.getDepName().equals(dto.getDepName())) {
                        cu += dto.getCounts();
                        a += dto.getAsset();
                        b += dto.getFund();
                        c += dto.getJustice();
                        d += dto.getGov();
                        e += dto.getExits();
                        f += dto.getPeer();
                    }
                }
                PartnerDepNameDTO ds = new PartnerDepNameDTO(info.getDepName(), info.getDepId(), a + b + c + d + e + f, a, b, e, c, f, d);
                all.add(ds);
            }

            //添加之后的部门进行排序
            Collections.sort(all, new Comparator<PartnerDepNameDTO>() {
                @Override
                public int compare(PartnerDepNameDTO o1, PartnerDepNameDTO o2) {
                    return o2.getCounts() - o1.getCounts();
                }
            });
            message.setData(all);
        }
        return message;
    }

    /**
     * 季度统计
     *
     * @param times
     * @param name
     * @return
     */
    @ApiOperation(value = "季度统计")
    @PostMapping("rankQuqrter")
    public JsonMessage rankQuqrter(String times, String name) {
        JsonMessage message = new JsonMessage();
        String[] ts = times.split("年");
        int quar = 0;
        if ("第1季度".equals(ts[1])) {
            quar = 1;
        } else if ("第2季度".equals(ts[1])) {
            quar = 2;
        } else if ("第3季度".equals(ts[1])) {
            quar = 3;
        } else if ("第4季度".equals(ts[1])) {
            quar = 4;
        }
        List<PartnerSelfDTO> mas = statService.partnerQuarterCount(ts[0], quar);
        Map<String, Integer> map = new HashMap<>();
        for (PartnerSelfDTO dos : mas
        ) {
            map.put(dos.getTrueName(), dos.getCounts());
        }
        if ("个人".equals(name)) {
            //sheet1
            List<PartnerSelfDTO> list1 = statService.partnerQuarter(ts[0], quar);
            for (PartnerSelfDTO p : list1
            ) {
                p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
            }
            //添加之后的部门进行排序
            Collections.sort(list1, new Comparator<PartnerSelfDTO>() {
                @Override
                public int compare(PartnerSelfDTO o1, PartnerSelfDTO o2) {
                    return o2.getCounts() - o1.getCounts();
                }
            });
            message.setData(list1);
        }
        if ("部门".equals(name)) {
            //sheet1
            List<PartnerSelfDTO> list1 = statService.partnerQuarter(ts[0], quar);
            List<PartnerDepNameDTO> all = new ArrayList<>();
            List<FormsInfo> list = statService.depId();


            for (PartnerSelfDTO p : list1
            ) {
                p.setCounts(p.getAsset() + p.getFund() + p.getJustice() + p.getGov() + p.getExits() + p.getPeer());
            }
            for (FormsInfo info : list
            ) {
                int a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, cu = 0;
                for (PartnerSelfDTO dto : list1
                ) {
                    if (info.getDepName().equals(dto.getDepName())) {
                        cu += dto.getCounts();
                        a += dto.getAsset();
                        b += dto.getFund();
                        c += dto.getJustice();
                        d += dto.getGov();
                        e += dto.getExits();
                        f += dto.getPeer();
                    }
                }
                PartnerDepNameDTO ds = new PartnerDepNameDTO(info.getDepName(), info.getDepId(), a + b + c + d + e + f, a, b, e, c, f, d);
                all.add(ds);
            }

            //添加之后的部门进行排序
            Collections.sort(all, new Comparator<PartnerDepNameDTO>() {
                @Override
                public int compare(PartnerDepNameDTO o1, PartnerDepNameDTO o2) {
                    return o2.getCounts() - o1.getCounts();
                }
            });
            message.setData(all);
        }


        return message;
    }


}
